package com.xul.controller;

import com.xul.entity.*;
import com.xul.service.*;
import com.xul.utils.MD5Utils;
import com.xul.utils.MapController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xul
 * @create 2021-06-15 13:53
 */

@Controller
public class IndexController {

    @Autowired
    private UserService userService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private ClazzService clazzService;

    @Autowired
    private SubjectService subjectService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private SectionService sectionService;

    @Autowired
    private ScoreService scoreService;

    @GetMapping("index")
    public String index() {
        return "index";
    }

    @GetMapping("info")
    public String info() {
        return "info";
    }

    @GetMapping("pwd")
    public String pwd() {
        return "pwd";
    }

    @PostMapping("pwd")
    @ResponseBody
    public Map<String, Object> pwd(String sourcePwd, String newPwd, String type, Integer id) {
        if ("1".equals(type)) {
            User user = userService.detail(id);
            if (user.getUserPwd().equals(MD5Utils.getMD5(sourcePwd))) {
                User entity = new User();
                entity.setId(id);
                entity.setUserPwd(MD5Utils.getMD5(newPwd));
                int update = userService.update(entity);
                if (update > 0) {
                    return MapController.getInstance().success().getMap();
                } else {
                    return MapController.getInstance().error().getMap();
                }
            } else {
                return MapController.getInstance().error("原密码错误").getMap();
            }
        }
        if ("2".equals(type)) {
            Teacher teacher = teacherService.detail(id);
            if (teacher.getTeacherPwd().equals(MD5Utils.getMD5(sourcePwd))) {
                Teacher entity = new Teacher();
                entity.setId(id);
                entity.setTeacherPwd(MD5Utils.getMD5(newPwd));
                int update = teacherService.update(entity);
                if (update > 0) {
                    return MapController.getInstance().success().getMap();
                } else {
                    return MapController.getInstance().error().getMap();
                }
            } else {
                return MapController.getInstance().error("原密码错误").getMap();
            }
        }
        if ("3".equals(type)) {
            Student student = studentService.detail(id);
            if (student.getStuPwd().equals(MD5Utils.getMD5(sourcePwd))) {
                Student entity = new Student();
                entity.setId(id);
                entity.setStuPwd(MD5Utils.getMD5(newPwd));
                int update = studentService.update(entity);
                if (update > 0) {
                    return MapController.getInstance().success().getMap();
                } else {
                    return MapController.getInstance().error().getMap();
                }
            } else {
                return MapController.getInstance().error("原密码错误").getMap();
            }
        }
        return MapController.getInstance().success().getMap();
    }

    @GetMapping("main")
    public String main(ModelMap modelMap) {
        // 1.系统数据概况
        List<Clazz> clazzes = clazzService.query(null);
        List<Subject> subjects = subjectService.query(null);
        List<Teacher> teachers = teacherService.query(null);
        List<Course> courses = courseService.query(null);
        List<Section> sections = sectionService.query(null);
        List<Student> students = studentService.query(null);
        modelMap.addAttribute("clazzCnt", clazzes.size());
        modelMap.addAttribute("teacherCnt", teachers.size());
        modelMap.addAttribute("subjectCnt", subjects.size());
        modelMap.addAttribute("courseCnt", courses.size());
        modelMap.addAttribute("sectionCnt", sections.size());
        modelMap.addAttribute("studentCnt", students.size());
        // 2.每个班级学生数量
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (Clazz clazz : clazzes) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", clazz.getClazzName());
            int cnt = 0;
            for (Student student : students) {
                if (student.getClazzId() == clazz.getId().intValue()) {
                    cnt++;
                }
            }
            map.put("cnt", cnt);
            mapList.add(map);
        }
        modelMap.addAttribute("mapList", mapList);
        // 3.查询各科平均成绩
        List<HashMap> mapList1 = scoreService.queryAvgScoreBySection();
        modelMap.addAttribute("mapList1", mapList1);
        return "main";
    }
}
